<?php


namespace app\controller\admin;


use app\BaseController;
use app\common\Consts;
use think\facade\Db;
use think\Request;
use app\models\user\AdminRoleModel;

class RoleController extends BaseController
{

    /**
     * 角色列表
     * @param Request $request
     * @return \think\response\Json
     */
    public function getRoleList(Request $request)
    {
//        try {
        $userInfo = $request->userInfo;
        $roleId = $userInfo->role_id;
        $where = [];
        $where[] = ['is_del', '=', 0];
        if ($roleId != Consts::ADMIN_ROLE_ID) {
            //$roleIdArr = get_child_role_id($roleId);
            //$where[] = ['id', 'in', $roleIdArr];
            $roleIdArr = AdminRoleModel::where('referrer', 'like', '%' . $roleId . '%')->column('id');
            $where[] = ['id', 'in', $roleIdArr];
        }
        $list = DB::name('admin_role')
            ->where($where)
            ->field('id,pid,role_name,is_del')
            ->order('pid asc,sort desc')
            ->select()
            ->toArray();
        $list = list_to_tree($list, 'id', 'pid', 'children');

        dump($list);
        die;
        return $this->success($list);
        //return $this->success($list);
//        } catch (\Exception $exception) {
//            return $this->error($exception->getMessage());
//        }
    }

    /**
     * 添加，编辑角色
     * @param Request $request
     * @return \think\response\Json
     */
    public function saveRoleInfo(Request $request)
    {
        try {
            $id = $request->param('id', 0);
            $roleName = $request->param('role_name');
            $isDel = $request->param('is_del', 0);
            $pid = $request->param('pid', 0);
            $sort = $request->param('sort', 0);
            if (!$id) {
                DB::name('admin_role')->insert(['role_name' => $roleName, 'pid' => $pid, 'sort' => $sort, 'is_del' => $isDel]);
            } else {
                DB::name('admin_role')->where('id', $id)->update(['role_name' => $roleName, 'pid' => $pid, 'sort' => $sort, 'is_del' => $isDel]);
            }
            return $this->success();
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    public function getSelfAndNextRoleList(Request $request)
    {
        try {
            $userInfo = $request->userInfo;
            $roleId = $userInfo->role_id;
            $where = [];
            $where[] = ['is_del', '=', 0];
            if ($roleId != Consts::ADMIN_ROLE_ID) {
                $roleIdArr = get_child_role_id($roleId);
                array_push($roleIdArr, $roleId);
                $where[] = ['id', 'in', $roleIdArr];
            }
            $list = DB::name('admin_role')
                ->where($where)
                ->order('sort', 'desc')
                ->select();
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    public function getNextRoleList(Request $request)
    {
        try {
            $userInfo = $request->userInfo;
            $roleId = $userInfo->role_id;
            $where = [];
            $where[] = ['is_del', '=', 0];
            if ($roleId != Consts::ADMIN_ROLE_ID) {
                $roleIdArr = get_child_role_id($roleId);
                $where[] = ['id', 'in', $roleIdArr];
            }
            $list = DB::name('admin_role')
                ->where($where)
                ->order('sort', 'desc')
                ->select();
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }
}
